package com.eningqu.aipen.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;
import com.luck.picture.lib.app.PictureAppMaster;
import com.luck.picture.lib.thread.PictureThreadUtils;

public class ToastUtils {
    private static final long TIME = 1000L;
    private static long lastClickTime;
    private static String mLastText;

    public ToastUtils() {
    }

    public static void showToast(final Context context, final String text) {
        if (!isFastDoubleClick() || !TextUtils.equals(text, mLastText)) {
            Context appContext = PictureAppMaster.getInstance().getAppContext();
            if (appContext == null) {
                appContext = context.getApplicationContext();
            }

            if (PictureThreadUtils.isInUiThread()) {
                Toast.makeText(appContext, text, Toast.LENGTH_LONG).show();
                mLastText = text;
            } else {
                PictureThreadUtils.runOnUiThread(new Runnable() {
                    public void run() {
                        Context appContext = PictureAppMaster.getInstance().getAppContext();
                        if (appContext == null) {
                            appContext = context.getApplicationContext();
                        }

                        Toast.makeText(appContext, text, Toast.LENGTH_LONG).show();
                        ToastUtils.mLastText = text;
                    }
                });
            }

        }
    }

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 1000L) {
            return true;
        } else {
            lastClickTime = time;
            return false;
        }
    }
}
